<?php

namespace App\Http\Controllers;

use App\Project;
use App\Colleage;
use File;
use Validator;

use App\Http\Controllers\Input;
use Illuminate\Http\Request;

class SiteController extends BaseController
{
    /**
     * 全部成员
     *
     * @return void
     */
    public function colleagueIndex()
    {
        $colleagues = Colleage::all();
        return $this->returnMsg('200', 'ok', $colleagues);
    }

    /**
     * 全部项目
     *
     * @return void
     */
    public function projectIndex()
    {
        $data = Project::all();
        return $this->returnMsg('200','ok', $data);
    }

    /**
     * 保存项目
     *
     * @param Request $request
     * @return void
     */
    public function store(Request $request)
    {
        $this->validate($request, [
			'pic' =>/* 'dimensions:ratio=4/3|*/'mimes:jpeg,png,gif'
        ]);

        $file = $request->file('pic');
		if(!$file){
			return $this->returnMsg('500','pic required');
		}
        $time = strtotime(date('Y-m-d H:i:s',time()));
        $file->move( public_path().'/upload/image/', $time.".".$file->getClientOriginalExtension());

        $input = $request->except('token','pic');
        $input['pic'] =$time.".".$file->getClientOriginalExtension();
        //dd($input);
        $result = Project::create($input);
        if($result){
            return  $this->returnMsg('200','ok');
        }else{
            return  $this->returnMsg('500','fail');
        }
    }

    /**
     * 单个项目
     *
     * @param [int] $id
     * @return void
     */
    public function show($id)
    {
        $project = Project::find($id);
        if (is_null($project)) {
            return $this->returnMsg('404','Not Found');
        }
        return  $this->returnMsg('200','ok',$project);
    }

    /**
     * 改
     *
     * @param Request $request
     * @param [type] $id
     * @return void
     */
    public function update(Request $request, $id)
    {
        $project = Project::find($id);
        $input = $request->except('token','pic');
        if( !is_null($request->file('pic')) ){
            $this->validate($request, [
                'pic' => /*'dimensions:ratio=4/3'*/'mimes:jpeg,png,gif'
            ]);
			$file = $request->file('pic');
			if(!$file){
				return $this->returnMsg('500','pic required');
			}
            $time = strtotime(date('Y-m-d H:i:s',time()));
            $file->move( public_path().'/upload/image/', $time.".".$file->getClientOriginalExtension());//保存文件
            unlink(public_path().'/upload/'.$project->pic);//删除文件

            $input['pic'] =$time.".".$file->getClientOriginalExtension();
        }
        $change = Project::findOrFail($id);
        
        $result = $change->update($input);
        if($result){
            return  $this->returnMsg('200','ok');
        }else{
            return  $this->returnMsg('500','fail');
        }
    }

    /**
     * delete
     *
     * @param Request $request
     * @return void
     */
    public function destroy(Request $request,$id)
    {
        //$id = $request->get('id');
        $project = Project::find($id);
        if ($project) {
			
            unlink(public_path().'/upload/'.$project->pic);//删除文件
            $result = $project->delete();
            if($result){
                return $this->returnMsg('200','ok');
            }else{
                return $this->returnMsg('500','fail');
            }
        }
        else {
            return $this->returnMsg('404','Not found');
        }
    }

}
